/*
 * Saphre - Suffix Arrays for Phrase Extraction
 * Copyright (C) 2013 
 * Dale Gerdemann Tübingen, Germany 
 * Niko Schenk Frankfurt am Main, Germany
 * All rights reserved.
 *
 * This program is free software: you can redistribute it and/or modify it under
 * the terms of the GNU General Public License as published by the Free Software
 * Foundation, either version 3 of the License, or (at your option) any later
 * version.
 *
 * This program is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
 * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
 * details.
 *
 * You should have received a copy of the GNU General Public License along with
 * this program. If not, see <http://www.gnu.org/licenses/>.
 *
 */
package collectors.impl;

import java.io.PrintWriter;
import util.sorting.Multiset;
import util.statistics.StatisticsHandler;
import collectors.api.Collector1;
import saphre.commands.CmdRepeatsParameters;
import util.Interval;
import saphre.core.Store;
import saphre.core.SuffixArray;

/**
 * Class which simulates lcp-interval tree traversal to collect general repeats
 * from a corpus based on user-specific parameter settings.
 *
 * It implements the Collector interface.
 *
 * @author Dale Gerdemann, Niko Schenk
 */
public class GeneralRepeatsCollector implements Collector1 {

    private Store store;
    private SuffixArray sa;
    private double D;
    private PrintWriter pw = null;
    private CmdRepeatsParameters repeatsParameters;
    
    private static int maximalscount = 0;
    private static int nonmaximalscount = 0;
    private static int supermaximalcount = 0;
    private static int nonsupermaximalcount = 0;

    /**
     * Assemble parameters and construct a GeneralRepeatsCollector.
     *
     * @param store
     * @param sa
     * @param top
     * @param pw
     * @param args
     */
    public GeneralRepeatsCollector(Store store, SuffixArray sa, PrintWriter pw, 
            CmdRepeatsParameters repeatsParameters) {
        this.store = store;
        this.sa = sa;
        //D = (double) store.numDocs();
        // Subtract 2! The program uses two initial and one final sentinel.
        D = (double) store.numDocs() - 2;
        this.pw = pw;

        this.repeatsParameters = repeatsParameters;
    }

    /**
     * Not used.
     *
     * @param pa
     */
    public void addPrefixArray(SuffixArray pa) {
    }

    /**
     * Not used - no repeated phrases.
     *
     * @param inter
     * @param parentLcp
     * @param lcp
     * @param depth
     */
    @Override
    public void preAdd(Interval inter, int parentLcp, int lcp, int depth) {
    }

    /**
     * Repeats are only found in this "add" method of the interface.
     *
     * @param inter
     * @param lcp
     * @param depth
     * @param docDist
     * @param leftContext
     */
    @Override
    public void add(Interval inter, int lcp, int depth, Multiset docDist,
            Multiset leftContext) {

        Multiset dd = inter.docDist(sa, store);
        int tf = inter.tf();
        int df = dd.size();

        // Limit on parameter values.
        // lcp is the length of the phrase.
        if (lcp >= repeatsParameters.getMinLength()
                && lcp <= repeatsParameters.getMaxLength()
                && tf >= repeatsParameters.getMinTermFrequency()) {

            int loc = sa.getSuftab()[inter.lb()];
            String ngram = store.toString(loc, (loc + lcp));

            if (ngram.startsWith(repeatsParameters.getStartsWith()) &&
                    ngram.contains(repeatsParameters.getContainsTerm())) {

                int ngramsize = loc - (loc - lcp);
                int[] generalngram = new int[ngramsize];
                int ngramindex = 0;
                // Collect this n-gram. Every array index is occupied by an individual token/character.
                for (int l = 0; l < ngramsize; l++) {
                    generalngram[l] = sa.getText()[loc + ngramindex];
                    ngramindex++;
                }

                // Compute mi and ridf.
                double miYamamotoChurch = StatisticsHandler.miYamamotoChurch(generalngram, sa);
                double miGeneral = StatisticsHandler.miGeneralNgram(generalngram, sa);
                double ridf = StatisticsHandler.ridf(tf, df, D);


                if (miYamamotoChurch >= repeatsParameters.getMinMutualInformationYamamotoChurch()
                        && miGeneral >= repeatsParameters.getMinMutualInformationGeneral()
                        && ridf >= repeatsParameters.getMinResidualInverseDocumentFrequency()) {

                    boolean isMaximal = false;
                    boolean isSupermaximal = false;

                    boolean leftVariable = false;
                    int b = store.text()[sa.getSuftab()[inter.lb()] - 1];
                    for (int i = inter.lb(); i <= inter.rb(); i++) {
                        int oneBefore = store.text()[sa.getSuftab()[i] - 1];
                        if (oneBefore != b) {
                            leftVariable = true;
                        }
                    }

                    if (leftVariable) {
                        isMaximal = true;
                        maximalscount++;
                        isSupermaximal = StatisticsHandler.isSupermaximal(inter, sa, lcp);
                        if (isSupermaximal) {
                            isSupermaximal = true;
                            supermaximalcount++;
                        } else {
                            nonsupermaximalcount++;
                        }
                    } else {
                        nonmaximalscount++;
                    }

                    // TODO: Add mutual information log entropy and ridf.

                    String rval =
                            ngram + "\t"
                            + isMaximal + "\t"
                            + isSupermaximal + "\t"
                            + ngramsize + "\t"
                            + tf + "\t"
                            + df + "\t"
                            + String.format("%.4g", miYamamotoChurch) + "\t"
                            + String.format("%.4g", miGeneral) + "\t"
                            + String.format("%.4g", ridf) + "\n";
                    if (repeatsParameters.isWriteToFile()) {
                        pw.print(rval);
                    }
                    if (repeatsParameters.isWriteToConsole()) {
                        System.out.print(rval);
                    }
                }
            }
        }
    }

    /**
     *
     * @param inter
     * @param lcp
     * @param depth
     * @param docDist
     * @param leftContext
     */
    @Override
    public void addTrivial(Interval inter, int lcp, int depth,
            Multiset docDist, Multiset leftContext) {
    }

    public static int getSupermaximalcount() {
        return supermaximalcount;
    }

    public static int getNonSupermaximalcount() {
        return nonsupermaximalcount;
    }

    public static int getMaximalcount() {
        return maximalscount;
    }

    public static int getNonMaximalcount() {
        return nonmaximalscount;
    }
}